import pygame
from types import SimpleNamespace
from common import (
    white,
    black,
    yellow,
    green,
    red,
    blue,
    gray,
    font_path,
    pink,
    go_next_level,
    get_score,
    add_score,
    get_current_level,
)

screen_width, screen_height = 1400, 850
image = pygame.image.load("./assets/jigsaw.png")
image_1 = pygame.image.load("./assets/jigsaw_1.png")
image_2 = pygame.image.load("./assets/jigsaw_2.png")
image_3 = pygame.image.load("./assets/jigsaw_3.png")
image_4 = pygame.image.load("./assets/jigsaw_4.png")
image_5 = pygame.image.load("./assets/jigsaw_5.png")
images = [image_1, image_2, image_3, image_4, image_5]
cur_image = image

button_text_font = pygame.font.Font(font_path, 20)

button_up = SimpleNamespace(
    label="up",
    rect=pygame.Rect(800, 400, 150, 50),
    color=pink,
    text="需要吸起",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_down = SimpleNamespace(
    label="down",
    rect=pygame.Rect(800, 500, 150, 50),
    color=green,
    text="需要打落",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_ok = SimpleNamespace(
    label="ok",
    rect=pygame.Rect(800, 600, 150, 50),
    color=blue,
    text="确认",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_submit = SimpleNamespace(
    label="submit",
    rect=pygame.Rect(800, 700, 150, 50),
    color=blue,
    text="提交",
    font_color=black,
    font=button_text_font,
    selected=-1,
)

button_lxj = SimpleNamespace(
    label="lxj",
    rect=pygame.Rect(1000, 400, 150, 50),
    color=yellow,
    text="LXJ",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_zxj = SimpleNamespace(
    label="zxj",
    rect=pygame.Rect(1000, 500, 150, 50),
    color=yellow,
    text="ZXJ",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_luxj = SimpleNamespace(
    label="luxj",
    rect=pygame.Rect(1000, 600, 150, 50),
    color=yellow,
    text="LUXJ",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_2dj = SimpleNamespace(
    label="2dj",
    rect=pygame.Rect(1000, 700, 150, 50),
    color=yellow,
    text="2DJ",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_txj = SimpleNamespace(
    label="txj",
    rect=pygame.Rect(1200, 400, 150, 50),
    color=yellow,
    text="TXJ",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_yxj = SimpleNamespace(
    label="yxj",
    rect=pygame.Rect(1200, 500, 150, 50),
    color=yellow,
    text="YXJ",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_none = SimpleNamespace(
    label="none",
    rect=pygame.Rect(1200, 600, 150, 50),
    color=gray,
    text="无",
    font_color=black,
    font=button_text_font,
    selected=-1,
)
button_go_next = SimpleNamespace(
    label="go_next",
    rect=pygame.Rect(1200, 700, 150, 50),
    color=gray,
    text="下一关",
    font_color=black,
    font=button_text_font,
    selected=-1,
)

buttons = [
    button_up,
    button_down,
    button_ok,
    button_submit,
    button_lxj,
    button_zxj,
    button_luxj,
    button_2dj,
    button_txj,
    button_yxj,
    button_none,
    button_go_next,
]
label_2_button = {button.label: button for button in buttons}
status_buttons = [
    button_up,
    button_down,
    button_lxj,
    button_zxj,
    button_luxj,
    button_2dj,
    button_txj,
    button_yxj,
    button_none,
]

text_notification = SimpleNamespace(
    label="notification",
    rect=pygame.Rect(1000, 200, 150, 150),
    text="请选择需要吸起或打落的信号机，然后点击确认。",
    font=pygame.font.Font(font_path, 25),
    font_color=red,
)

text_error = SimpleNamespace(
    label="error",
    rect=pygame.Rect(1000, 250, 150, 150),
    text="",
    font=pygame.font.Font(font_path, 25),
    font_color=red,
)

score = 0
level = 0

level_2_text = [
    "选择正确按钮及状态，办理正线接车时正确亮灯",
    "选择正确按钮及状态，办理通过进路时正确亮灯",
    "选择正确按钮及状态，平时状态时正确亮灯",
    "选择正确按钮及状态，办理侧线接车时正确亮灯",
    "选择正确按钮及状态，开放引导信号时正确亮灯",
]

level_2_condition = [
    {
        "ok": {
            "up": 1,
            "lxj": 1,
            "zxj": 1,
        },
        "submit": {
            "down": 1,
            "txj": 1,
            "luxj": 1,
        },
    },
    {
        "ok": {
            "up": 1,
            "lxj": 1,
            "zxj": 1,
            "txj": 1,
        },
        "submit": {
            "down": 1,
            "none": 1,
        },
    },
    {
        "ok": {
            "up": 1,
            "none": 1,
        },
        "submit": {
            "down": 1,
            "lxj": 1,
        },
    },
    {
        "ok": {
            "up": 1,
            "lxj": 1,
            "2dj": 1,
        },
        "submit": {
            "down": 1,
            "luxj": 1,
            "zxj": 1,
            "txj": 1,
        },
    },
    {
        "ok": {
            "up": 1,
            "yxj": 1,
        },
        "submit": {
            "down": 1,
            "lxj": 1,
        },
    },
]


def reset():
    global score, cur_image
    score = 0
    cur_image = image

    for button in buttons:
        button.selected = -1

    text_notification.text = ""
    text_error.text = ""


def alert():
    text_notification.text = "恭喜你，回答错误。点击下一关继续挑战！"
    text_error.text = "再好好巩固一下进站信号机点灯电路的知识吧！"
    for button in buttons:
        button.selected = 0
    add_score(-20)


def judge(condition):
    for sb in status_buttons:
        if sb.label in condition:
            if sb.selected != condition[sb.label]:
                return False
        else:
            if sb.selected == 1:
                return False

    return True


def draw(screen):
    global score, voltage, fire, fire_time, level, cur_image
    condition = level_2_condition[level]

    # 绘制背景
    screen.fill(white)

    # 绘制title
    font = pygame.font.Font(font_path, 30)
    text = font.render(
        f"当前得分（{get_score()}）关卡{get_current_level() + 1}：{level_2_text[level]}",
        True,
        black,
    )
    text_rect = text.get_rect(center=(screen_width // 2, 50))
    screen.blit(text, text_rect)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            for button in buttons:
                if button.rect.collidepoint(event.pos):
                    if button in status_buttons:
                        button.selected = 1 if button.selected != 1 else 0

                    elif button.label == "ok" and button.selected == -1:
                        button.selected = 1
                        flag = judge(condition["ok"])
                        if not flag:
                            alert()
                        else:
                            text_notification.text = (
                                "请选择需要打落的信号机，然后点击提交。"
                            )
                            for button in status_buttons:
                                button.selected = -1
                    elif (
                        button.label == "submit"
                        and label_2_button["ok"].selected != -1
                        and button.selected == -1
                    ):
                        button.selected = 1
                        flag = judge(condition["submit"])
                        if not flag:
                            alert()
                        else:
                            cur_image = images[level]
                            add_score(20)
                            text_notification.text = (
                                "恭喜你，回答正确。点击下一关继续挑战！"
                            )

                    elif (
                        button.label == "go_next"
                        and label_2_button["submit"].selected != -1
                        and label_2_button["ok"].selected != -1
                    ):
                        level += 1
                        go_next_level()

    # draw image
    screen.blit(pygame.transform.scale(cur_image, (750, 700)), (0, 100))
    # draw notification
    text = text_notification.font.render(
        text_notification.text, True, text_notification.font_color
    )
    screen.blit(text, text.get_rect(center=text_notification.rect.center))
    text = text_error.font.render(text_error.text, True, text_error.font_color)
    screen.blit(text, text.get_rect(center=text_error.rect.center))

    for _, button in enumerate(buttons):
        font = button.font
        if button.selected == 1:
            bt = button.text + "(选中)"
        else:
            bt = button.text
        text = font.render(bt, True, button.font_color)
        text_rect = text.get_rect(center=button.rect.center)
        # 绘制按钮
        pygame.draw.rect(screen, button.color, button.rect)
        # 绘制文字
        screen.blit(text, text_rect)
